package com.aos.erp.gov.ex.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.gov.ex.enums.TaskSyncStatus;
import com.aos.erp.gov.ex.enums.TaskSyncType;
import com.aos.erp.gov.ex.mapper.GovExSyncTaskMapper;
import com.aos.erp.gov.ex.model.GovExSyncTask;
import com.aos.erp.gov.ex.service.GovExSyncTaskService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class GovExSyncTaskServiceImpl extends ServiceImpl<GovExSyncTaskMapper, GovExSyncTask> implements GovExSyncTaskService {

    @Override
    public GovExSyncTask getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void addBean(GovExSyncTask bean) {
        baseMapper.insert(bean);
    }

    @Override
    public PageBean<GovExSyncTask> queryByPage(long pageIndex, long pageSize) {

        IPageBean<GovExSyncTask> pageBean = baseMapper.createBizPage(pageIndex, pageSize);
        Condition condition = new Condition(GovExSyncTask.class);
        condition.orderByDesc("id");

        return (PageBean<GovExSyncTask>) baseMapper.selectPage(pageBean, condition.getWrapper());
    }

    @Override
    public void saveNewAccount(long ticketId, String account) {

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.DAY_OF_YEAR, -1);
        addBean(makeDefaultTask(ticketId, account, TaskSyncType.keeplive, TaskSyncStatus.close, calendar));
        addBean(makeDefaultTask(ticketId, account, TaskSyncType.yk, calendar));
        addBean(makeDefaultTask(ticketId, account, TaskSyncType.ykqx, TaskSyncStatus.close, calendar));
        addBean(makeDefaultTask(ticketId, account, TaskSyncType.yktz, TaskSyncStatus.close, calendar));
        addBean(makeDefaultTask(ticketId, account, TaskSyncType.cj, calendar));
        addBean(makeDefaultTask(ticketId, account, TaskSyncType.ylrtj, TaskSyncStatus.close, calendar));
        addBean(makeDefaultTask(ticketId, account, TaskSyncType.ylr, calendar));
    }

    private GovExSyncTask makeDefaultTask(long ticketId, String account, TaskSyncType taskSyncType, Calendar calendar) {
        GovExSyncTask bean = new GovExSyncTask();
        bean.setTicketId(ticketId);
        bean.setAccount(account);
        bean.setStatus(TaskSyncStatus.work);
        bean.setTaskType(taskSyncType);
        bean.setLastStartTime(calendar.getTime());
        bean.setLastEndTime(calendar.getTime());
        bean.setTimeLine(bean.getLastEndTime());
        return bean;
    }

    private GovExSyncTask makeDefaultTask(long ticketId, String account, TaskSyncType taskSyncType, TaskSyncStatus status, Calendar calendar) {
        GovExSyncTask bean = new GovExSyncTask();
        bean.setTicketId(ticketId);
        bean.setAccount(account);
        bean.setStatus(status);
        bean.setTaskType(taskSyncType);
        bean.setLastStartTime(calendar.getTime());
        bean.setLastEndTime(calendar.getTime());
        bean.setTimeLine(bean.getLastEndTime());
        return bean;
    }

    @Override
    public List<GovExSyncTask> getTopNeedSync(Date minTime, TaskSyncType type) {

        Condition condition = new Condition(GovExSyncTask.class);

        condition.le("lastStartTime", minTime);//小于某个时间点的
        condition.eqFiled("taskType", type);
        condition.eqFiled("status", TaskSyncStatus.work);
        condition.orderByAsc("lastStartTime");//越久的越先刷新

        IPage page = baseMapper.createPlusPage(1, 10);
        ((Page) page).setOptimizeCountSql(false);//不count查询

        baseMapper.selectPage(page, condition.getWrapper());
        return page.getRecords();
    }

    @Override
    public void updateLastStartTime(long id, Date time) {

    }

    @Override
    public void updateLastEndTime(long id, Date time, int num) {

    }
}